/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.qxypt.io
 *
 * 版权所有，侵权必究！
 */

package xw.qxypt.modules.job.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xw.qxypt.common.utils.ALiSmsUtil;
import xw.qxypt.modules.baseinfo.entity.*;
import xw.qxypt.modules.baseinfo.service.*;
import xw.qxypt.modules.sys.entity.SysUserEntity;
import xw.qxypt.modules.sys.service.SysUserService;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 路面探测器预警短信推送
 *
 * 为spring bean的名称
 *
 * @author Mark sunlightcs@gmail.com
 */
@Component("sendAlertMessageTask")
public class SendAlertMessageTask implements ITask {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private LhpWaysectRoleService waysectRoleService;
	@Autowired
	private LhpUserWaysectroleRelationService userWaysectroleRelationService;
	@Autowired
	private LhpDataRoadDetectorService roadDetectorService;
	@Autowired
	private LhpDataVisibilityService visibilityService;
	@Autowired
	private SysUserService userService;
	@Autowired
	private LhpMessageService messageService;
	@Autowired
	private LhpInstallationPointService installationPointService;


	@Override
	public void run(String params){
		logger.debug("预警短信推送定时任务正在执行，参数为：{}", params);
		//查询所有用户
		List<SysUserEntity> userList=userService.list();
		//查询所有安装点
		List<LhpInstallationPointEntity> pointList=installationPointService.list();
		//查询所有路段角色
		List<LhpWaysectRoleEntity> roleList = waysectRoleService.list();
		SimpleDateFormat sf =new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Calendar c = Calendar.getInstance();
		c.add( Calendar.MINUTE, -5);
		for (LhpWaysectRoleEntity role:roleList) {
			//查询路段角色下的路面数据
			List<LhpDataRoadDetectorEntity> roadDataList=roadDetectorService.queryRoadDataByRoleId(c.getTime(), role.getId());
			//查询路段角色下的能见度数据
			List<LhpDataVisibilityEntity> visibilityList=visibilityService.queryVisibilityDataByRoleId(c.getTime(),role.getId());
			//安装点名称赋值
			/*for (LhpInstallationPointEntity point:pointList ) {
				//路面数据
				for (LhpDataRoadDetectorEntity roadData:roadDataList) {
					if(point.getId().equals(roadData.getPointId())){
						roadData.setPointName(point.getPointName());
					}
				}
				//能见度数据


			}*/
			//根据角色查询关联用户
			List<LhpUserWaysectroleRelationEntity> relationList=userWaysectroleRelationService.list(
					new QueryWrapper<LhpUserWaysectroleRelationEntity>()
							.eq("role_id",role.getId())
			);
			for(SysUserEntity user:userList){
				for(LhpUserWaysectroleRelationEntity relation:relationList){
					if(user.getUserId()==Long.parseLong(relation.getUserId()) && user.getMobile()!=null && !"".equals(user.getMobile())){
						//路面信息推送
						for(LhpDataRoadDetectorEntity en:roadDataList) {
							Calendar c1 = Calendar.getInstance();
							c1.add( Calendar.HOUR, -2);
							//如果是冰 雪 霜天气
							if("35".equals(en.getRoadCondition()) ) {
								//查询两小时内是否发送过
								List<LhpMessageEntity> messageList=messageService.list(
										new QueryWrapper<LhpMessageEntity>()
												.eq("point_id",en.getPointId())
												.eq("data_type","roadCondition")
												.ge("send_time",c1.getTime())
								);
								//如果没查到数据则此安装点没有发送过
								if(messageList==null ||messageList.isEmpty()) {
									LhpMessageEntity message = new LhpMessageEntity();
									message.setDataType("roadCondition");
									message.setPointId(en.getPointId());
									//message.setReceContent("{\"time\":\""+sf.format(en.getDataTime())+"\", \"pointname\":\""+en.getPointName()+"\", \"context\":\"道路结冰，摩擦系数"+en.getFriction()+"\"}");
									message.setRecePepole(user.getUsername());
									message.setReceTelNum(user.getMobile());
									message.setSendTime(new Date());
									messageService.save(message);
									//发送短信
									//ALiSmsUtil.SendSms(user.getMobile(), "{\"time\":\""+sf.format(en.getDataTime())+"\", \"pointname\":\""+en.getPointName()+"\", \"context\":\"道路结冰，摩擦系数"+en.getFriction()+"\"}");
								}
							}else if("40".equals(en.getRoadCondition()) ) {
								//查询两小时内是否发送过
								List<LhpMessageEntity> messageList=messageService.list(
										new QueryWrapper<LhpMessageEntity>()
												.eq("point_id",en.getPointId())
												.eq("data_type","roadCondition")
												.ge("send_time",c1.getTime())
								);
								//如果没查到数据则此安装点没有发送过
								if(messageList==null ||messageList.isEmpty()) {
									LhpMessageEntity message = new LhpMessageEntity();
									message.setDataType("roadCondition");
									message.setPointId(en.getPointId());
									//message.setReceContent("{\"time\":\""+sf.format(en.getDataTime())+"\", \"pointname\":\""+en.getPointName()+"\", \"context\":\"道路积雪，摩擦系数"+en.getFriction()+"\"}");
									message.setRecePepole(user.getUsername());
									message.setReceTelNum(user.getMobile());
									message.setSendTime(new Date());
									messageService.save(message);
									//发送短信
									//ALiSmsUtil.SendSms(user.getMobile(), "{\"time\":\""+sf.format(en.getDataTime())+"\", \"pointname\":\""+en.getPointName()+"\", \"context\":\"道路积雪，摩擦系数"+en.getFriction()+"\"}");
								}
							}else if("45".equals(en.getRoadCondition()) ) {
								//查询两小时内是否发送过
								List<LhpMessageEntity> messageList=messageService.list(
										new QueryWrapper<LhpMessageEntity>()
												.eq("point_id",en.getPointId())
												.eq("data_type","roadCondition")
												.ge("send_time",c1.getTime())
								);
								//如果没查到数据则此安装点没有发送过
								if(messageList==null ||messageList.isEmpty()) {
									LhpMessageEntity message = new LhpMessageEntity();
									message.setDataType("roadCondition");
									message.setPointId(en.getPointId());
									//message.setReceContent("{\"time\":\""+sf.format(en.getDataTime())+"\", \"pointname\":\""+en.getPointName()+"\", \"context\":\"道路结霜，摩擦系数"+en.getFriction()+"\"}");
									message.setRecePepole(user.getUsername());
									message.setReceTelNum(user.getMobile());
									message.setSendTime(new Date());
									messageService.save(message);
									//发送短信
									//ALiSmsUtil.SendSms(user.getMobile(), "{\"time\":\""+sf.format(en.getDataTime())+"\", \"pointname\":\""+en.getPointName()+"\", \"context\":\"道路结霜，摩擦系数"+en.getFriction()+"\"}");
								}
							}else if(Double.parseDouble(en.getRoadSurfaceTemp())<0 && !"10".equals(en.getRoadCondition())) {
								//查询两小时内是否发送过
								List<LhpMessageEntity> messageList=messageService.list(
										new QueryWrapper<LhpMessageEntity>()
												.eq("point_id",en.getPointId())
												.eq("data_type","roadSurfaceTemp")
												.ge("send_time",c1.getTime())
								);
								//如果没查到数据则此安装点没有发送过
								if(messageList==null ||messageList.isEmpty()) {
									LhpMessageEntity message = new LhpMessageEntity();
									message.setDataType("roadSurfaceTemp");
									message.setPointId(en.getPointId());
									//message.setReceContent("{\"time\":\""+sf.format(en.getDataTime())+"\", \"pointname\":\""+en.getPointName()+"\", \"context\":\"路面温度"+en.getRoadSurfaceTemp()+"℃,路面潮湿\"}");
									message.setRecePepole(user.getUsername());
									message.setReceTelNum(user.getMobile());
									message.setSendTime(new Date());
									messageService.save(message);
									//发送短信
									//ALiSmsUtil.SendSms(user.getMobile(), "{\"time\":\""+sf.format(en.getDataTime())+"\", \"pointname\":\""+en.getPointName()+"\", \"context\":\"路面温度"+en.getRoadSurfaceTemp()+"℃,路面潮湿\"}");
								}
							}
						}
						//能见度预警推送
						for (LhpDataVisibilityEntity visibility:visibilityList ) {
							Calendar c2 = Calendar.getInstance();
							c2.add( Calendar.HOUR, -1);
							//能见度数值小于500
							if (Double.parseDouble(visibility.getVisibility())<=500){
								//查询一小时内是否发送过
								List<LhpMessageEntity> messageList=messageService.list(
										new QueryWrapper<LhpMessageEntity>()
												.eq("point_id",visibility.getPointId())
												.eq("data_type","visibility")
												.ge("send_time",c2.getTime())
								);
								//如果没查到数据则此安装点没有发送过
								if(messageList==null ||messageList.isEmpty()) {
									LhpMessageEntity message = new LhpMessageEntity();
									message.setDataType("visibility");
									message.setPointId(visibility.getPointId());
									//message.setReceContent("{\"time\":\""+sf.format(visibility.getDataTime())+"\", \"pointname\":\""+visibility.getPointName()+"\", \"context\":\"能见度"+visibility.getVisibility()+"米\"}");
									message.setRecePepole(user.getUsername());
									message.setReceTelNum(user.getMobile());
									message.setSendTime(new Date());
									messageService.save(message);
									//发送短信
									//ALiSmsUtil.SendSms(user.getMobile(), "{\"time\":\""+sf.format(visibility.getDataTime())+"\", \"pointname\":\""+visibility.getPointName()+"\", \"context\":\"能见度"+visibility.getVisibility()+"米\"}");
								}

							}

						}
					}
				}
			}
		}

	}
}
